<template>
    <div class='commonList' v-loading="loading">
        <search @search-event="searchEvent" :searchStr="searchStr"  ref="searchs" v-if="hidenSearch!='Y'"></search>
        <el-row v-if="buttonJson.length<=4&&(buttonJson.length>0||tabJson.length>0)" class='elrows'>
            <el-col :span="12">
                 <button-bar :buttonJson="buttonJson" @btn-click="btnClick"></button-bar>
            </el-col>
             <el-col :span="12">
                 <tab-bar :tabJson="tabJson" @tab-event="tabEvent"></tab-bar>
            </el-col>
        </el-row>
        <el-row v-if="buttonJson.length>4" class='elrows2'>
            <el-col :span="24">
                 <button-bar :buttonJson="buttonJson" @btn-click="btnClick"></button-bar>
            </el-col>
            
        </el-row>
         <el-row v-if="buttonJson.length>4"  class='elrows'>
            <el-col :span="24">
                 <tab-bar :tabJson="tabJson" @tab-event="tabEvent"></tab-bar>
            </el-col>
        </el-row>
        <div class='tables' ref="tables"> 
            <simple-table :model="model" :buttons="buttonJsonRow"  :company="company" :listField="list" @openWin="openWin"   selectType="multipleTable" ref="tableList"  @empty="empty" :noPage="noPage" @rowClick="rowClick"   @rowDbClick="rowDbClick" @rowMultiClick="rowMultiClick" :sKey="sKey" :sKey2="sKey2" :url="url" :tabJson="tabJson2" :searchJson="searchStr2" :listname="listname" v-if="showTable&&listType=='simple'" :appid="appid"  :model_key="model_key"></simple-table>
            
        </div>
    </div> 
  
</template>
 
<script>
    import Vue  from 'vue'
    import SimpleTable  from './SimpleTable.vue'
    import ButtonBar  from './ButtonBar.vue'
    import search  from './search.vue'
    import tabBar  from './TabBar.vue'
    export default {
        props:['sKey','sKey2','company','searchs','listnames','hidenSearch','searchJson','appid','model_key','model'],
        name: "commonList2",
        components: {
            SimpleTable,
            ButtonBar,
            search,
            tabBar
        },
        data(){
            return {
                listname:this.listnames,
                list:[],
                showTable:false,
                searchStr:[],
                buttonJson:[],
                buttonJsonRow:[],
                tabJson:[],
                tabJson2:[],
                searchStr2:[],
                curRow:null,
                curRows:null,
                loading:true,
                noPage:false,
                sKeyd:'',
                sKeyd2:'',
                sKeyc:'',
                sKeyc2:'',
                listType:'simple',
                url:''/**请求后台url */
            }
        },
        create(){
            
        },
        mounted(){
            
           this.loadData();

        },
        methods:{
            empty(data){
                 this.$emit("empty","");
            },
            rowDbClick(data){
                this.curRow=JSON.parse(data);
                this.curRow.rowIndex=this.index;
                this.$emit("rowDbClick",JSON.stringify(this.curRow));
            },
            getScrollTop(){
                return this.$refs.tableList.getScrollTop();
            },
            setScrollTop(top){
                this.$refs.tableList.setScrollTop(top);
            },
            initData(){
                this.$refs.tableList.initData();
            },
            loadData(){ 
                let sef=this;
              
                this.$post(this.appid,'api/view/commonListJson',{
                    sKey:this.sKey,
                    sKey2:this.sKey2,
                    company:escape(escape(this.company)),
                    search:escape(escape(this.searchs)),
                    listname:escape(escape(this.listname)),
                    model:escape(escape(this.model)),
                    
                    appid:this.appid,
                    key:escape(this.model_key)
                }).then(data=>{
                    sef.list=data[0].list;
                    sef.searchStr=data[0].searchStr;
                    
                    sef.tabJson=data[0].tabJson;
                    sef.url=data[0].url;
                    sef.searchStr2=data[0].searchStr;
                    //没有这句，double第一次加载的时候，第一行数据明细列表会显示所有数据
                    if(sef.searchJson.length>0)
                            sef.searchStr2=sef.searchJson
                    sef.tabJson2=data[0].tabJson;
                    sef.showTable=true;
                    sef.loading=false;
                    if(data[0].noPage=="Y")
                        sef.noPage=true;
                    
                    sef.buttonJson=[];
                    let buttonJsons=data[0].buttonJson;
                    if(buttonJsons){
                        for(let i=0;i<buttonJsons.length;i++){
                            if(buttonJsons[i].types!="row"){
                                sef.buttonJson.push(buttonJsons[i]);
                            }else{
                                sef.buttonJsonRow.push(buttonJsons[i]);
                            }
                        }
                    }
                    
                })
            },
            loadData2(){ //切换listname的时候使用
                let sef=this;
                let sKey=this.sKey;
                let sKey2=this.sKey2;
                
                if(this.listname.indexOf("modelId")!=-1){
                    let modelIds=this.listname.split(":");
                    this.sKeyd2=modelIds[1];
                    this.listname="";
                    this.sKeyd="";
                    this.listType="double";
                }else{
                    this.listType="simple";
                }
                if(this.listType=="simple"){
                    this.$post(this.appid,'api/view/commonListJson',{
                        sKey:sKey,
                        sKey2:sKey2,
                        company:escape(escape(this.company)),
                        search:escape(escape(this.searchs)),
                        listname:escape(escape(this.listname))
                    }).then(data=>{
                        sef.list=data[0].list;
                        sef.searchStr=data[0].searchStr;
                        sef.url=data[0].url;
                        sef.searchStr2=data[0].searchStr;
                        sef.showTable=true;
                        sef.loading=false;
                        if(data[0].noPage=="Y")
                            sef.noPage=true;
                        
                        if(sef.searchJson.length>0)
                            sef.searchStr2=sef.searchJson
                            
                    // sef.$refs.tableList.loadData();
                    })
                }
                
            },
            isShowTable(){
                this.showTable=true;
            },
            searchEvent(data){
                this.searchStr2=JSON.parse(data);
              //  this.$refs.tableList.loadData();
            },
            tabEvent(data){
                let datas=JSON.parse(data);
                let isok=true;
                for(let i=0;i<datas.length;i++){
                    let isDateSelect=datas[i].isDateSelect;
                    if(datas[i].listname&&datas[i].listname!=this.listname){
                        this.listname=datas[i].listname;
                        isok=false;
                        this.loadData2();
                        if(isDateSelect=="Y"){
                            this.tabJson2=JSON.parse(data);
                        }
                    }
                        
                }
                if(isok)
                    this.tabJson2=JSON.parse(data);
            },
            openWin(data){
              
              try{
                  this.$parent.openWin(data);
              }catch(e){
                
                   this.$emit("openWin",data);
              }
              
          },
            btnClick(data){
                let item=JSON.parse(data);
                let wb_isEdit=item.wb_isEdit;
                let key=item.key;
                let isOpen=true;
                let wb_msg=item.wb_msg;
                if(wb_isEdit=="Y"&&this.curRow==null){
                    isOpen=false;
                    this.$message.error('请先选择一条数据');
                }else if(wb_isEdit=="Y"){
                    let sId=this.curRow[key];
                    let url=item.url;
                    url=url.indexOf("?")==-1 ? url+"?sId="+sId:url+"&sId="+sId;
                    item.url=url;

                }
                if(isOpen&&wb_msg==""){
                    this.$parent.openWin(JSON.stringify(item));
                }
                
               // this.$parent.$parent.postMessage(data,'*');
            },
            rowClick(data){
                this.curRow=JSON.parse(data);
                this.$emit("rowClick",data);
                
                
            },
            rowMultiClick(data){
                this.curRows=JSON.parse(data);
                this.$emit("rowMultiClick",data);
            },
            goSearch(){
                console.log(this.searchStr2,"sjlsjdlf")
                console.log(this.searchJson,"sjlsjdlf")
                this.$refs.tableList.loadData();
            },
            setSearch(json){
                    console.log("searchsjosnsosjodfjlsdjflds11111111111")
                this.searchStr2=json;
            }
        },
        watch:{
            searchJson:{
                 handler: function (newVal) {
                    console.log("searchsjosnsosjodfjlsdjflds")
                    this.searchStr2=newVal;
                },      
                deep: true    //深度监听
               
            }
        }
    }
</script>

<style scoped  src="../../assets/css/commonList.css">
</style>